package com.egao.wecanfly;

/**
 * @author : Doman
 * @date : 2019-1-22 上午11:17:13
 * @version ：
 */


import com.egao.common.core.damon.GuavaUitl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Velocity生成静态页面
 * @author wanghjbuf
 */
public class WeCanFlyAPI {
	private static final String DEF_ENCODING = "utf-8";

	public static void main(String []args) throws Exception{

		WeCanFlyAPI w = new WeCanFlyAPI();
		w.generatorFileforBeetl();
	}

	public void generatorFileforBeetl() throws Exception{
		Properties prop = PropertiesLoad.getProperties();
		String database = prop.getProperty("database");
		String tableName = prop.getProperty("tableName");
		String tableNameJavaEntity = prop.getProperty("tableNameJavaEntity");
		WeCanFlyAPI generHtmlForVelocity = new WeCanFlyAPI();
		StringBuffer sql = new StringBuffer("select table_name,table_comment,create_time from information_schema.TABLES ");
		sql.append(" where TABLE_SCHEMA='"+database+"' ");
		sql.append(" and instr(table_name,\""+tableName+"\")>0 ");
		JDBCHelperWeCanFly dbHelper = new JDBCHelperWeCanFly();
		ResultSet resultSet = dbHelper.executeResultSet(sql.toString());

		String tableComment = "";String create_time = "";
		if (resultSet.next())
		{
			tableName = resultSet.getString("table_name");
			tableComment = resultSet.getString("table_comment");
			create_time = resultSet.getString("create_time");
			if(create_time.length()>2){
				create_time = create_time.substring(0,create_time.length()-2);
			}
		}
		sql = new StringBuffer("select COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT ");
		sql.append("from information_schema.COLUMNS ");
		sql.append("where table_name = '"+tableName+"' and table_schema = '"+database+"'");
		sql.append(" order by ORDINAL_POSITION ");
		resultSet = dbHelper.executeResultSet(sql.toString());
		List<String> columnNameList;List<String> columnNameNoCAMELList;List<String> dataTypeList;List<String> columnCommentList;
		columnNameList = new ArrayList<String>();columnNameNoCAMELList = new ArrayList<String>();dataTypeList = new ArrayList<String>();columnCommentList = new ArrayList<String>();
		while (resultSet.next())
		{
			columnNameList.add(GuavaUitl.LOWER_CAMEL(resultSet.getString("COLUMN_NAME")));
			columnNameNoCAMELList.add(resultSet.getString("COLUMN_NAME"));
			dataTypeList.add(resultSet.getString("DATA_TYPE"));
			columnCommentList.add(resultSet.getString("COLUMN_COMMENT"));
		}
		dbHelper.close();

		for (int i = 0; i < columnNameList.size(); i ++) {
			String COLUMN_NAME = columnNameList.get(i);
			String DATA_TYPE = dataTypeList.get(i);
			String COLUMN_COMMENT = columnCommentList.get(i);
			System.out.println(COLUMN_NAME+ " "+DATA_TYPE+" "+COLUMN_COMMENT);
		}
		System.out.println("");
		System.out.println("");
		System.out.println("");
		System.out.println("");
		for (int i = 0; i < columnNameList.size(); i ++) {
			String COLUMN_NAME = columnNameList.get(i);
			String DATA_TYPE = dataTypeList.get(i);
			String COLUMN_COMMENT = columnCommentList.get(i);
			System.out.println(COLUMN_NAME+ ":"+COLUMN_COMMENT);
		}
		System.out.println("");
		System.out.println("");
		System.out.println("");
		System.out.println("");
		for (int i = 0; i < columnNameList.size(); i ++) {
			String COLUMN_NAME = columnNameList.get(i);
			String DATA_TYPE = dataTypeList.get(i);
			String COLUMN_COMMENT = columnCommentList.get(i);
			System.out.println(COLUMN_NAME+ ":");
		}
		System.out.println("");
		System.out.println("");
		System.out.println("");
		System.out.println("");
		for (int i = 0; i < columnNameNoCAMELList.size(); i ++) {
			String COLUMN_NAME = columnNameNoCAMELList.get(i);
			String DATA_TYPE = dataTypeList.get(i);
			String COLUMN_COMMENT = columnCommentList.get(i);
			System.out.println(COLUMN_NAME+ " "+DATA_TYPE+" "+COLUMN_COMMENT);

		}
//		for (int i = 0; i < columnNameList.size(); i ++) {
//			String COLUMN_NAME = columnNameList.get(i);
//			String DATA_TYPE = dataTypeList.get(i);
//			String COLUMN_COMMENT = columnCommentList.get(i);
//			System.out.print(COLUMN_NAME+ "=");
//
//		}

		/*System.out.println("@ApiImplicitParams({");
		for (int i = 0; i < columnNameList.size(); i ++) {
			String COLUMN_NAME = columnNameList.get(i);
			String DATA_TYPE = dataTypeList.get(i);
			String COLUMN_COMMENT = columnCommentList.get(i);
			System.out.println("	@ApiImplicitParam(name = \""+COLUMN_NAME+"\",value = \""+COLUMN_COMMENT+"\",paramType = \"body\",dataType = \"String\",required = true),");
		}
		System.out.println("})");*/
	}
}